import { observable, action, computed, toJS } from 'mobx';
import { Base64 } from 'js-base64';
import { Tools, Toast, Modal, Bubble } from 'weaver-mobile';
import { Reply, Comment } from 'weaver-mobile-page';
import * as taskBase from '../../api/task/taskMain'
const getLabel = Tools.getLabel;
const alert = Modal.alert;

export default class TaskCommentStore {

    @observable commentList = [];

    @action
    changeCommentList = (commentList) => {
        this.commentList = commentList
    }

    @computed
    get replyStore() {
        return new Reply.Store({ getAttachList: this.getAttachList, getAtConfig: this.getAtConfig }); // 没有 + 和 @ 功能，可以直接 new Reply.Store();
    }

    @observable taskId = "";  //当前任务id
    @observable
    taskDetail = {} // 当前任务详情

    @action
    changeTaskDetail = (taskDetail) => {
        this.taskDetail = taskDetail
    }
    @observable pageSize = 5; //分页大小
    @observable pageNum = 0;  //当前页数
    @observable canDelete = false; //能否删除
    @observable replyFocus = false;
    @observable replyid = ""; //回复评论的id

    @action
    changeReplyid = (replyid) => {
        this.replyid = replyid
    }

    @observable mainId = ""; //主评论的id
    @observable toUserId = "";//被回复的用户的id
    @observable allCommentList = [];//所有的评论数据

    @action
    changeAllCommentList = (allCommentList) => {
        this.allCommentList = allCommentList
    }

    @observable
    sending = false   // 反馈发送状态

    @observable commentStore = new Comment.Store({
        getComment: this.getComment,
        operate: this.operate,
        // getChild: () => {console.log("child...")},
        getChild: null
    });

    @observable
    category = ""

    @action
    changeCategory = (category) => {
        this.category = category
    }

    @observable
    replyAttachDatas = {}

    @action
    changeReplyAttachDatas = (replyAttachDatas) => {
        this.replyAttachDatas = replyAttachDatas
    }

    @action
    setTaskId = (taskId) => {
        this.taskId = taskId;
    }

    @action
    changeCommentList = (commentList) => {
        this.commentList = commentList
    }

    @computed
    get getAttachList() {
        return () => {
            return new Promise((resolve) => {
                resolve({
                    attachList: [
                        {
                            key: 'doc',
                            type: 'browser',
                            path: '/taskHome/taskDetail/' + this.taskId,
                            config: {
                                type: 37,
                                isSingle: false,
                            },
                            iconType: 'document-big',
                            iconBgColor: '#F3B04C',
                            text: getLabel(58, '文档'),
                        },
                        { // 浏览按钮类型组件type为 browser
                            key: 'flow', // 字符串
                            type: 'browser',
                            path: '/taskHome/taskDetail/' + this.taskId,
                            config: {
                                type: 152,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: 'flow',
                            iconBgColor: '#61A8F5',
                            text: getLabel(18015, '流程'),
                        },
                        { // 浏览按钮类型组件type为 browser
                            key: 'customer', // 字符串
                            type: 'browser',
                            path: '/taskHome/taskDetail/' + this.taskId,
                            config: {
                                type: 7,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: 'customer',
                            iconBgColor: '#61A8F5',
                            text: getLabel(136, '客户'),
                        },
                        {
                            key: 'project',
                            type: 'browser',
                            path: '/taskHome/taskDetail/' + this.taskId,
                            config: {
                                type: 8,
                                isSingle: false,
                            },
                            iconType: 'relevant-project',
                            iconBgColor: '#F3B04C',
                            text: getLabel(101, '项目'),
                        },
                        { // 上传类型组件type为upload
                            key: 'file',
                            type: 'upload',
                            path: '/taskHome/taskDetail/' + this.taskId,
                            config: {
                                multiSelection: true,
                                uploadUrl: "/api/doc/upload/uploadFile2Doc?category=" + this.category
                            },
                            iconType: 'file',
                            iconBgColor: '#F3B04C',
                            text: getLabel('156', '附件'),
                        },
                    ],
                });
            })
        }
    }

    @computed
    get getAtConfig() {
        return () => {
            return new Promise((resolve) => {
                resolve({
                    atConfig: {
                        key: 'at',
                        path: '/taskHome/taskDetail/' + this.taskId,
                        config: {
                            type: 16,
                            isSingle: false,
                        },
                    },
                });
            })
        }
    }

    @action("添加反馈")
    addTaskFeedBack = (params) => {
        this.sending = true
        params.mainId = this.getCommentMainId(params.replyid)
        return taskBase.addTaskFbInfo(params).then(action(res => {
            this.sending = false;
            if (res.status) {
                Bubble.success(getLabel(390839, '反馈成功'), 1)
                this.commentList = [];
                this.replyid = "";
                // this.commentStore.getDatas();
                if (res.mobileComment) {
                    this.commentStore.doOperateReply({
                        replyId: params.mainId,
                        parentId: null,
                        reply: res.mobileComment
                    })
                } else {
                    this.commentStore.getDatas();
                }
                this.doClearReply();
            } else {
                Bubble.error(res.msg || getLabel('25009', '失败'), 1)
            }
        }));
    }

    // @action("删除反馈")
    // delTaskFbInfo = (params) => {
    //     this.loading = true;
    //     taskBase.taskOperation(params).then(action(res => {
    //         this.loading = false;
    //         if (res.api_status) {
    //             Toast.success(getLabel(20461, '删除成功'), 1);
    //         } else {
    //             Toast.fail(res.msg || getLabel('25009', '失败'), 1);
    //         }
    //     }));
    // }

    // // 实际接口调用的方法
    @action("获取评论")
    getComment = () => {
        let params = {}
        params.taskId = this.taskId;
        params.viewdate = this.taskDetail.viewdate
        params.pageSize = this.pageSize;
        params.pageNum = this.pageNum;
        if (this.commentList && this.commentList.length > 0) {
            let key = this.commentList.length - 1;
            params.lastId = this.commentList[key].replyId;
        }
        return taskBase.taskListComment(params).then(action(res => {
            if (res.status) {
                this.commentList = res.replyList;
                for (let i = 0; i < res.replyList.length; i++) {
                    this.allCommentList.push(res.replyList[i])
                }
                return res
            }
        }))
    }

    @action
    operate = (item) => {
        const { type, data, parent } = item;
        if (type.type == 'blog-comment') {
            this.replyFocus = true;
            this.replyid = data.replyId;
            // this.mainId = data.replyMainId ? data.replyMainId :  data.replyId;  //没有主评论，则默认当前评论为主评论
            // this.toUserId = data.creater.id;
        } else if (type.type == 'delete') {
            alert(getLabel(388758, '确认要删除吗？'), '', [
                { text: getLabel(31129, '取消') },
                {
                    text: getLabel(33703, '确认'),
                    onPress: () => {
                        let params = {
                            taskId: this.taskId,
                            fbid: item.data.replyId,
                            operation: "delFbInfo"
                        }

                        return taskBase.taskOperation(params).then(action(res => {
                            this.commentStore.doOperateReply({
                                replyId: data.replyId,
                                parentId: parent ? parent.replyId : '',
                                reply: null,
                            })
                        }));

                    }
                },
            ])
            // this.delTaskFbInfo(params)
        }
    }

    /**
     * 获取主评论的id
     */
    getCommentMainId = (id) => {
        let mainId = "";
        let len = this.allCommentList.length;
        for (let i = 0; i < len; i++) {
            if (id == this.allCommentList[i].replyId) {
                mainId = this.allCommentList[i].replyId;
            } else {
                let childList = this.allCommentList[i].childReply;
                if (childList) {
                    for (let j = 0; j < childList.length; j++) {
                        if (id == childList[j].replyId) {
                            mainId = childList[j].replyMainId;
                        }
                    }
                }
            }
        }
        return mainId;
    }

    /**
     * 清空回复区
     */
    doClearReply = () => {
        this.replyStore.doClear();
        this.replyFocus = false;
    }

    @action
    taskListComment = (params) => {
        this.loading = true;
        taskBase.taskListComment(params).then(action(res => {
            this.loading = false;
            if (res.status) {
                return res.data;
            } else {
                // message.error(res.msg || getLabel('25009','失败'));
            }
        }));
    }

}